//
//  CGModifyPasswordViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/18.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGModifyPasswordViewController.h"

@interface CGModifyPasswordViewController ()<UIAlertViewDelegate>

@property(nonatomic,strong)CGRegistTextView *oldPasswordText;          //旧密码
@property(nonatomic,strong)CGRegistTextView *modifyPasswordText;       //新密码
@property(nonatomic,strong)CGRegistTextView *surePasswordText;         //确认密码
@property(nonatomic,strong)UIButton *modifyButton;                     //注册

@property(nonatomic,strong)UIWindow *coverWindow;
@property(nonatomic,strong)UIView *shadeView;
@property(nonatomic,strong)UIActivityIndicatorView *activity;

@end

#define TEXT_MARGIN 10    //文本框的边距
@implementation CGModifyPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.title = @"修改密码";
    
    [self layoutUI];
}

-(void)layoutUI{
    
    CGRect aFrame = CGRectMake(20, 10, SCREEN_WIDTH - 20 - 20, 44);
    //旧密码
    self.oldPasswordText = [[CGRegistTextView alloc]initWithFrame:aFrame andImageName:@"login_password_icon"];
    self.oldPasswordText.textField.placeholder = @"请输入您的旧密码";
    self.oldPasswordText.textField.delegate = self;
    self.oldPasswordText.textField.secureTextEntry = YES;
    [self.view addSubview:self.oldPasswordText];
    
    //新密码
    aFrame.origin.y = CGRectGetMaxY(self.oldPasswordText.frame) + TEXT_MARGIN;
    self.modifyPasswordText = [[CGRegistTextView alloc]initWithFrame:aFrame andImageName:@"login_password_icon"];
    self.modifyPasswordText.textField.placeholder = @"请输入新密码";
    self.modifyPasswordText.textField.delegate = self;
    self.modifyPasswordText.textField.secureTextEntry = YES;
    [self.view addSubview:self.modifyPasswordText];
    
    //确认密码
    aFrame.origin.y = CGRectGetMaxY(self.modifyPasswordText.frame) + TEXT_MARGIN;
    self.surePasswordText = [[CGRegistTextView alloc]initWithFrame:aFrame andImageName:@"login_password_icon"];
    self.surePasswordText.textField.placeholder = @"请确认密码";
    self.surePasswordText.textField.delegate = self;
    self.surePasswordText.textField.secureTextEntry = YES;
    [self.view addSubview:self.surePasswordText];
    

    //立即修改Button
    aFrame.origin.y = CGRectGetMaxY(self.surePasswordText.frame) + 20;
    aFrame.origin.x = 20;
    aFrame.size.width = SCREEN_WIDTH - 40;
    self.modifyButton = [[UIButton alloc]initWithFrame:aFrame];
    [self.modifyButton setBackgroundImage:[UIImage imageNamed:@"login_btn_bg"] forState:UIControlStateNormal];
    [self.modifyButton setTitle:@"立即修改" forState:UIControlStateNormal];
    self.modifyButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.view addSubview:self.modifyButton];
    //添加点击事件
    [self.modifyButton addTarget:self action:@selector(modifyButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)modifyButtonClick{
    //1.有效性验证,去除字符串两端的空格
    NSString *oldPassword = [self.oldPasswordText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *newPassowrd = [self.modifyPasswordText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *passowrdToo = [self.surePasswordText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (oldPassword.length == 0) {
        [Tools alertWithTitle:@"温馨提示" andMessage:@"请输入您的旧密码!"];
        return;
    }

    if (![newPassowrd isEqualToString:passowrdToo] || newPassowrd.length == 0) {
        [Tools alertWithTitle:@"温馨提示" andMessage:@"新密码输入有误,请重新输入!"];
        return;
    }

     [self popShadeView];   //弹出延时框
    
    //发送请求
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"oldPWD"] = self.oldPasswordText.textField.text;
    parameters[@"newPWD"] = self.modifyPasswordText.textField.text;
    parameters[@"account"] = [CGAccountDefault shareAccount].account;
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/main/updateUserPWD.app" parameters:parameters success:^(id responseData) {
        
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"result:%@",result);

        if ([result rangeOfString:@"account"].location != NSNotFound) {
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"恭 喜" message:@"密码修改成功!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertView show];
          
        }else{
            
            [Tools alertWithTitle:@"温馨提示" andMessage:@"用户不存在或密码错误"];
        }
        
        [self hidShadeView];
        return ;
        
    } failure:^(NSError *error) {
        
        [self hidShadeView];
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
        return;
    }];


}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.view.transform = CGAffineTransformIdentity;
    }];
    
    return YES;
}

#pragma mark 弹出网络延时加载框
-(void)popShadeView{
    
    self.shadeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.shadeView.backgroundColor = [UIColor blackColor];
    self.shadeView.alpha = 0.3;
    self.coverWindow = [UIApplication sharedApplication].keyWindow;
    [self.coverWindow addSubview:self.shadeView];
    
    //添加一个等待进度轮
    self.activity = [[UIActivityIndicatorView alloc]init];
    self.activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    //self.activity.color = [UIColor blueColor];
    CGPoint center = self.view.center;
    self.activity.center = center;
    [self.activity startAnimating];
    [self.coverWindow addSubview:self.activity];
    
}

#pragma mark 取消网络延时加载框
-(void)hidShadeView{
    
    self.shadeView.hidden = YES;
    [self.activity stopAnimating];
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


@end
